var urlRoot = '../model/';

var id;

$(function() {
	closeFrame();
	id = getQueryString("id");
	refreshInfo();
	refreshLog();
});


var frameType = 0;
var frameParam = undefined

function showFrame(type, title, info, param, value) {
	frameType = type;
	frameParam = param;

	$("#popFrame").css("display", "block");

	$("#frameShow").html(info);
	$("#frameTitle").html(title);

	if (value)
		$("#frameInput").val(value);
	else
		$("#frameInput").val("");

	if (type >= 0)
		$("#frameInput").css("display", "block");
	else
		$("#frameInput").css("display", "none");
}

function closeFrame() {
	$("#popFrame").css("display", "none");
	$("#frameShow").html("");
	$("#frameTitle").html("");
	$("#frameInput").val("");
}

function frameOk() {
	delLog(frameParam);
	closeFrame();
}

var nowApi;

function refreshInfo() {

	jQuery.ajax({
		type: 'GET',
		contentType: 'application/json',
		url: urlRoot + 'getapi?fullInfo=false&id=' + id,
		dataType: 'json',
		async: false,
		success: function(data) {
			if (checkApiErrorInfo(data, "获取API信息出错")) {
				nowApi = data.data;
				$("#apiTitle").html("[" + id + "] " + nowApi.title);
			}
			else {
				if (data.stay < 0) {
					toPage('./index.html');
				}
			}

			if (data.info)
				log(data.info);
		},
		error: function(e) {
			log("返回结果出错： " + JSON.stringify(e));
		}
	});
}

function refreshLog() {
	if (!nowApi)
		return;

	jQuery.ajax({
		type: 'GET',
		contentType: 'application/json',
		url: urlRoot + 'getapilog?api=' + id,
		dataType: 'json',
		async: false,
		success: function(data) {
			if (checkApiErrorInfo(data, "获取API信息出错")) {

				var logs = data.data;
				console.log(data);

				var logStr = "";
				var cellHeight = "100px";
				var preStyle = "style='white-space: pre-wrap; resize: vertical;height:" + cellHeight + "'";

				for (var i = 0; i < logs.length; i++) {
					var item = logs[i];
					logStr += "<tr id='logItem_" + item.id + "'>";

					logStr += "<td>" + item.time + "</td>";
					logStr += "<td>" + item.url + "</td>";
					logStr += "<td style='text-align: left;'><pre " + preStyle + ">" + item.prompt + "</pre></td>";
					logStr += "<td style='text-align: left;'><pre " + preStyle + ">" + item.param + "</pre></td>";
					logStr += "<td style='text-align: left;'><pre " + preStyle + ">" + item.input + "</pre></td>";
					logStr += "<td style='text-align: left;'><pre " + preStyle + ">" + item.response + "</pre></td>";
					logStr += "<td>" + item.cost + "</td>";
					logStr += "<td><span class='btn-del btn btn32 ripple' onclick='showDel(" + item.id + ")'></span></td>";

					logStr += "</tr>";
				}

				$("#logHolder").html(logStr);
			}

			if (data.info)
				log(data.info);
		},
		error: function(e) {
			log("返回结果出错： " + JSON.stringify(e));
		}
	});
}

function returnApi() {
	toPage("./api.html?id=" + id);
}

function showDel(logId) {
	showFrame(-1, '删除日志', '确认是否删除该日志: ', logId);
}

function delLog(logId) {
	var item = '#logItem_' + logId;

	jQuery.ajax({
		type: 'GET',
		contentType: 'application/json',
		url: urlRoot + "dellog?api=" + id + "&id=" + logId,
		dataType: 'json',
		async: false,
		success: function(data) {
			if (checkApiErrorInfo(data, "获取API信息出错")) {
				$(item).remove();
			}

			if (data.info)
				log(data.info);
		},
		error: function(e) {
			log("返回结果出错： " + JSON.stringify(e));
		}
	});
}